package pl.hexar.action.offert;

import java.io.Serializable;
import java.util.Date;

import javax.annotation.PostConstruct;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;



@Name("lastOffert")
@Scope(ScopeType.STATELESS)
@AutoCreate 
public class LastOffert implements Serializable {

	private Date lastModify = new Date();

	@In
	EntityManager entityManager;
	

	@Logger
	Log log;
	
	public void init() {
		try {
			log.info("LastOffert init...");
			setLastModify((Date) entityManager.createQuery(
					"select max(o.modDate) from Offert o").getSingleResult());
			log.info("LastOffert inited" + getLastModify());
		} catch (Exception e) {
			log.info("LastOffer !!error!!"+ e);
			e.printStackTrace();
		}
	}

	public void setLastModify(Date lastModify) {
		this.lastModify = lastModify;
	}

	public Date getLastModify() {
		return this.lastModify;
	}

	public void refresh() {
		try {
			log.info("LastOffert refresh ");
			setLastModify((Date) entityManager.createQuery(
					"select max(o.modDate) from Offert o").getSingleResult());
			
			log.info("LastOffert refreshed " + getLastModify());
		} catch (Exception e) {
			log.error("LastOffer !!error!! {0}", e.toString());
		}
	}
}
